package com.guragura;

import java.util.Stack;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.guragura.screens.CreditScreen;
import com.guragura.screens.GameScreen;
import com.guragura.screens.GuraScreen;
import com.guragura.screens.HelpScreen;
import com.guragura.screens.MainMenuScreen;
import com.guragura.screens.SettingScreen;
import com.guragura.screens.StartScreen;
import com.guragura.util.GuraDialogInterface;
import com.guragura.util.GuraFont;
import com.guragura.util.GuraGameData;
import com.guragura.util.GuraMusic;
import com.guragura.util.GuraSettings;
import com.guragura.util.GuraSound;
import com.guragura.util.IGuraVibrator;

/**
 * This Guragura class extend from {@link Game} class. Guragura was made special
 * for this game
 * 
 * @author hartono
 * 
 */
public class Guragura extends Game {

	public final static int START_SCREEN = 0;
	public final static int MAIN_MENU = 1;
	public final static int GAME = 2;
	public static final int SETTING_SCREEN = 3;
	public static final int HELP_SCREEN = 4;
	public static final int CREDIT_SCREEN = 5;
	
	
	public int gameState;

	public GuraDialogInterface dialogInterface;

	public IGuraVibrator vibrator;
	
	public SpriteBatch batch;
	public BitmapFont font;
	public Preferences preferences;
	public AssetManager assetManager;
	public Stack<GuraScreen> sScreens;
	public TextureAtlas atlasInterface;
	public TextureAtlas atlasStart;
	
	public GuraSound sound;
	public GuraMusic music;
	public GuraFont guraFont;
	
	public GuraScreen mainMenuScreen;
	public GuraScreen starterScreen;
	public GuraScreen creditScreen;
	public GuraScreen helpScreen;
	public GuraScreen settingScreen;
	public GuraScreen gameScreen;

	public Guragura() {
		// TODO Auto-generated constructor stub
		vibrator= new IGuraVibrator() {
			
			@Override
			public void vibrate() {
				// TODO Auto-generated method stub
				
			}
		};
	}

	public Guragura(GuraDialogInterface dialogInterface) {
		// TODO Auto-generated constructor stub
		this.dialogInterface = dialogInterface;
	}

	public Guragura(int flag, IGuraVibrator guravibrator) {
		// TODO Auto-generated constructor stub		
		this.vibrator = guravibrator;
	}

	
	/*
	 * Initialize all objects here (non-Javadoc)
	 * 
	 * @see com.badlogic.gdx.ApplicationListener#create()
	 */
	@Override
	public void create() {
		// init common object for game
		batch = new SpriteBatch();
		font = new BitmapFont();
		sScreens = new Stack<GuraScreen>();
		assetManager = new AssetManager();

		// init screen
		mainMenuScreen = new MainMenuScreen(this);
		starterScreen = new StartScreen(this);
		creditScreen = new CreditScreen(this);
		helpScreen = new HelpScreen(this);
		settingScreen = new SettingScreen(this);
//		gameScreen = new GameScreen(this);

		loadSettings();
		sScreens.push(mainMenuScreen);
		pushScreen(starterScreen);
	}

	/**
	 * PopScreen will pop out the {@link GuraScreen} from
	 * {@link Guragura#sScreens}. Call this method will change the screen to the
	 * previous one.
	 * 
	 * @return GuraScreen
	 */
	public GuraScreen popScreen() {
		GuraScreen screen = sScreens.pop();
		if (screen != null) {
			if (!sScreens.empty()) {
				setScreen(sScreens.peek());
			} else {
				// exitGame();
			}
		}
		return screen;
	}

	/**
	 * Change current screen
	 * 
	 * @param screen
	 *            This screen will be pushed to screen stack.
	 */
	public void pushScreen(GuraScreen screen) {
		sScreens.push(screen);
		setScreen(screen);
	}

	/**
	 * This method will load all settings.
	 */
	public void loadSettings() {
		GuraSettings.loadSettings();
		GuraGameData.loadData();
	}

	/**
	 * This method will save all settings.
	 */
	public void saveSettings() {
		GuraSettings.saveSettings();
		GuraGameData.saveSettings();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		saveSettings();
		super.pause();
		batch.dispose();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		loadSettings();
		super.resume();
		batch = new SpriteBatch();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		batch.dispose();
		assetManager.dispose();
		atlasInterface.dispose();
		atlasStart.dispose();
	}

	/**
	 * Exit game
	 */
	public void exitGame() {
		if (dialogInterface == null) {
			System.out.println("DIALOG INTERFACE NULL");
			dispose();
			System.exit(0);
		} else {
			System.out.println("DIALOG INTERFACE NOT NULL");
			dialogInterface.dialogExit();
		}
	}

	/**
	 * Initialize game resources after asset have been successfully loaded.
	 */
	public void initGame() {
		try {
//			atlasInterface = assetManager.get("atlas", TextureAtlas.class);
		} catch (Exception e) {

		}
		mainMenuScreen.initFromRes();
		settingScreen.initFromRes();
		creditScreen.initFromRes();
		helpScreen.initFromRes();
//		gameScreen.initFromRes();

	}

	/**
	 * memainkan game
	 */
	public void playGame() {
		gameScreen = new GameScreen(this);
		gameScreen.initFromRes();
		pushScreen(gameScreen);
	}
	

	public void showHelp() {
		if (dialogInterface == null) {
			pushScreen(helpScreen);
		}else{
			dialogInterface.dialogHelp();
		}
	}

	public void showCredit() {
		pushScreen(creditScreen);

	}

	public void showSetting() {
		pushScreen(settingScreen);
	}
	
	/**akan dipanggil pada {@link GameScreen#exitScreen()} untuk menampilkan score 
	 * setelah user keluar dari game screen 
	 * 
	 * @param newHs
	 * @param score
	 * @param prevscore
	 */
	public void showScore(boolean newHs,int score, int prevscore){
		((MainMenuScreen)mainMenuScreen).showScorePanel(newHs,score,prevscore);
	}
}
